package cn.lichenes.controller;

import cn.lichenes.entity.Address;
import cn.lichenes.entity.User;
import cn.lichenes.log.SystemLog;
import cn.lichenes.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import javax.servlet.http.HttpSession;

@Controller
public class AddressController {

    @Autowired
    private AddressService addressService;

    @SystemLog(value = "用户添加收货地址",status = "user")
    @PostMapping("/address")
    public String addAddress(String province, String city, String name, String phone, HttpSession session){
        User user = (User) session.getAttribute("user");
        Address address = new Address();
        address.setuAddressProvince(province);
        address.setuAddressDetail(city);
        address.setuName(name);
        address.setuPhone(phone);
        address.setUser(user);
        boolean flag = addressService.add(address);
        return "redirect:/account";
    }

    @SystemLog(value = "用户删除收货地址",status = "user")
    @DeleteMapping("/address/{id}")
    public String deleteAddress(@PathVariable(value = "id")Integer id){
        boolean delete = addressService.delete(id);
        return "redirect:/account";
    }

    @SystemLog(value = "用户修改收货地址",status = "user")
    @PutMapping("/address/{id}")
    public String updateAddress(@PathVariable(value = "id")Integer id,
                                String province, String city, String name, String phone, HttpSession session){
        User user = (User) session.getAttribute("user");
        Address address = new Address();
        address.setaId(id);
        address.setuAddressProvince(province);
        address.setuAddressDetail(city);
        address.setuName(name);
        address.setuPhone(phone);
        address.setUser(user);
        boolean flag = addressService.add(address);
        return "redirect:/account";
    }
}
